#!/usr/bin/perl
#dependence package.
use Socket;
use Proc::Daemon;
use Sys::Hostname;
use Net::Address::IP::Local;
use LWP::UserAgent;
use Switch;

# cpan -i LWP::Protocol::https Net::Address::IP::Local Proc::Daemon
#
#configure the target URL.
$delay = 300;
$log   = "/var/log/dnsdynamic.log";
$lock  = "/tmp/dnsdynamic_lock";
$pidf  = "/var/run/dnsdynamic.pid";

sub logTime {    #the time string log.
    ( $week, $mon, $day, $ht, $year ) = split( " ", localtime( time() ) );
    my $str = $year . " " . $mon . " " . $day . "  " . $ht . "    ";
    $str;
}

sub tarUrl {
    my $usr = "centny%40gmail.com";
    my $pwd = "wsh123456";
    my $tar = "sco.dnsdynamic.com";

    # Get the local system's IP address that is "en route" to "the internet":
    my $address = Net::Address::IP::Local->public;
    my $url     =
      sprintf( "https://%s:%s\@www.dnsdynamic.org/api/?hostname=%s&myip=%s",
        $usr, $pwd, $tar, $address );
    $url;
}

#start process.
sub start {
    if ( -e $lock ) {
        print "the daemon aleady running\n";
        exit 0;
    }
    my $daemon = Proc::Daemon->new( work_dir => './' );
    my $cpid = $daemon->Init;
    unless ($cpid) {

        # code executed only by the child ...
        #write the lock file.
        open( F, ">>" . $log );
        print F &logTime . "start running\n";
        close(F);

        #the main loop.
        while (1) {
            open( F, ">>" . $log );
            my $user_agent = new LWP::UserAgent;
            my $url        = &tarUrl;
            print F &logTime . "require:" . $url . "\n";
            my $request = new HTTP::Request( 'POST', $url );
            my $response = $user_agent->request($request);
            if ( $response->is_success() ) {
                print F &logTime
                  . "require success,response:"
                  . $response->{_content} . "\n";
            }
            else {
                print F &logTime
                  . "require faild,response code:"
                  . $response->code() . "\n";
            }
            close(F);
            if($response->is_success()){
                sleep $delay;
            }else{
                sleep 10;
            }
        }
    }
    if ( $cpid > 0 ) {
        print sprintf( "start timing web require success(PID:%d)...\n", $cpid );
        `touch $lock`;
        `echo $cpid >$pidf`;
    }
    else {
        print "start timing web require faild";
    }
}

#stop process.
sub stop {
    if ( !-e $lock ) {
        print "the daemon is not running\n";
        exit 0;
    }
    open( F, ">>" . $log );
    print F &logTime . "stopping daemon\n";
    close(F);
    open( F, $pidf );
    my $cpid = <F>;
    `kill $cpid`;
    `rm -f $lock`;
    `echo >$pidf`;
    close(F);

    if ( -e $lock ) {
        print "stop the daemon faild!\n";
        exit 0;
    }
}

#clear the log/lock file.
sub clear {
    `rm -f $log`;
    `rm -f $lock`;
}

#switch method.
switch ( @ARGV[0] ) {
    case "start" {
        &start;
    }
    case "stop" {
        &stop;
    }
    case "clear" {
        &clear;
    }
    else {
        print "Usage:cmd [start|stop|clear]\n";
    }
}
